//
//  EpointDateUtils.m
//  EpointFrame5
//
//  Created by 李亚春 on 14-2-8.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "EpointDateUtils.h"

@implementation EpointDateUtils

+(NSString *)getCurrentDateStr:(NSString *)format
{
    NSDate *  senddate=[NSDate date];
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:format];
    NSString *  locationString=[dateformatter stringFromDate:senddate];
    return locationString;
}

+(NSDate *)convertStr2Date:(NSString *)timestr format:(NSString *)format
{
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
    [inputFormatter setDateFormat:format];
    NSDate* inputDate = [inputFormatter dateFromString:timestr];
    return inputDate;
}

+(NSString *)convertDate2FormatStr:(NSDate *)_date format:(NSString *)format
{
    NSString* date;
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:format];
    date = [formatter stringFromDate:_date];
    return date;
}

+ (NSString *)convertStr2FromStr:(NSString *)_str oldFormat:(NSString *)oldFormat withNewFormat:(NSString *)newFormat {
    NSDate *date = [EpointDateUtils convertStr2Date:_str format:oldFormat];
    NSString *string = [EpointDateUtils convertDate2FormatStr:date format:newFormat];
    return string;
}

+ (NSString *)convertToMailListNeededFormat:(NSString *)_str {
    return [self convertStr2FromStr:_str oldFormat:@"yyyy/M/d H:mm:ss" withNewFormat:@"yy/MM/dd HH:mm"];
}

+ (NSString *)convertToMailListNeededFormat2:(NSString *)_str {
    return [self convertStr2FromStr:_str oldFormat:@"yyyy/M/d H:mm:ss" withNewFormat:@"yy/MM/dd HH:mm:ss"];
}

+ (NSString *)convertToCalendarNeedFormat:(NSDate *)date {
    return [EpointDateUtils convertDate2FormatStr:date format:@"yyyy.MM.dd"];
}

+ (NSString *)getDay:(NSDate *)date {
    return [EpointDateUtils convertDate2FormatStr:date format:@"d"];
}

+ (NSString *)convertDate2FormatYMDHMStr:(NSDate *)_date {
    return [self convertDate2FormatStr:_date format:@"yyyy-MM-dd HH:mm"];
}

+ (NSDate *)convertString2YMDHMDate:(NSString *)_str {
    return [self convertStr2Date:_str format:@"yyyy-MM-dd HH:mm"];
}

+ (NSDate *)convertString2YMDHMSDate:(NSString *)_str {
    return [self convertStr2Date:_str format:@"yyyy-MM-dd HH:mm:ss"];
}

+ (int)getNumberOfDaysOneYear:(NSDate *)date{
    
    NSCalendar *calender = [NSCalendar currentCalendar];
    
    NSRange range = [calender rangeOfUnit:NSDayCalendarUnit
                     
                                   inUnit: NSYearCalendarUnit
                     
                                  forDate: date];
    return range.length;
}

+(int)getNumberOfDaysOneMonth:(NSDate *)date{
    
    NSCalendar *calender = [NSCalendar currentCalendar];
    
    NSRange range = [calender rangeOfUnit:NSDayCalendarUnit
                     
                                   inUnit: NSMonthCalendarUnit
                     
                                  forDate: date];
    return range.length;
}

+ (NSDate *)getDateDifferWithDate:(NSDate *)date differDays:(int)differDay {
    NSTimeInterval timeInter = [date timeIntervalSince1970];
    NSTimeInterval differTimeInter = timeInter + 24*60*60*differDay;
    NSDate *differDate = [NSDate dateWithTimeIntervalSince1970:differTimeInter];
    return differDate;
}

+ (NSInteger)getWeekWithDate:(NSDate*)date
{
    unsigned unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit;
    NSCalendar *gregorian = [NSCalendar currentCalendar];
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:date];
    NSInteger week = comps.weekday;
    return week;
}

+ (NSArray *)getMonAndSunInWeekWithDifferWeeks:(int)differWeek {
    return [self getMonAndSunInWeekWithDate:[NSDate date] DifferWeeks:differWeek];
}

+ (NSArray *)getMonAndSunInWeekWithDate:(NSDate *)date DifferWeeks:(int)differWeek {
    
    NSDate *differDay = [self getDateDifferWithDate:date differDays:7*differWeek];
    NSInteger weekday = [self getWeekWithDate:differDay];
    int mondayDiffer = 2 - weekday;
    if (weekday == 1) {
        mondayDiffer = -6;
    }
    int sundayDiffer = 8 - weekday;
    if (weekday == 1) {
        sundayDiffer = 0;
    }
    NSDate *monday = [self getDateDifferWithDate:differDay differDays:mondayDiffer];
    NSDate *sunday = [self getDateDifferWithDate:differDay differDays:sundayDiffer];
    NSArray *array = @[monday, sunday];
    return array;
}

+(NSString *)getWeekNameByDate:(NSDate *)date
{
    return [self getWeekName:[self getWeek:date]];
}

+(int)getWeek:(NSDate *)date
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSInteger unitFlags = NSYearCalendarUnit |
    NSMonthCalendarUnit |
    NSDayCalendarUnit |
    NSWeekdayCalendarUnit |
    NSHourCalendarUnit |
    NSMinuteCalendarUnit |
    NSSecondCalendarUnit;
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    comps = [calendar components:unitFlags fromDate:date];
    return [comps weekday];
}

+(NSString *)getWeekName:(int)week
{
    NSString *weekstr;
    switch (week) {
        case 1:
            weekstr = @"日";
            break;
        case 2:
            weekstr = @"一";
            break;
        case 3:
            weekstr = @"二";
            break;
        case 4:
            weekstr = @"三";
            break;
        case 5:
            weekstr = @"四";
            break;
        case 6:
            weekstr = @"五";
            break;
        case 7:
            weekstr = @"六";
            break;
        default:
            weekstr = @"";
            break;
    }
    return weekstr;
}

@end
